package com.jd.dl.web.interceptor;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.StrutsStatics;
import org.omg.CORBA.UserException;
import org.springframework.util.StringUtils;

import com.jd.dl.pojo.User;
import com.jd.dl.service.UserService;
import com.jd.dl.web.BackstageLoginAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {
	public static final String USER_SESSION_KEY = "wallet.session.admin";
	public static final String COOKIE_REMEMBERME_KEY = "wallet.cookie.rememberme";
	public static final String GOING_TO_URL_KEY = "GOING_TO";

	private UserService userService;

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		if(invocation.getAction() instanceof BackstageLoginAction){
			return invocation.invoke();
		}
		
		ActionContext actionContext = invocation.getInvocationContext();
		HttpServletRequest request = (HttpServletRequest) actionContext
				.get(StrutsStatics.HTTP_REQUEST);

		Map session = actionContext.getSession();
		if (session != null && session.get(USER_SESSION_KEY) != null) {
			return invocation.invoke();
		}

		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie cookie : cookies) {
				if (COOKIE_REMEMBERME_KEY.equals(cookie.getName())) {
					String value = cookie.getValue();
					if (!"".equals(value)) {
						String[] split = value.split("==");
						String userName = split[0];
						String password = split[1];
						try {
							User user = userService.login(userName, password);
							session.put(USER_SESSION_KEY, user);
						} catch (Exception e) {
							setGoingToURL(session, invocation);
							return "login";
						}
					} else {
						setGoingToURL(session, invocation);
						return "login";
					}
					return invocation.invoke();
				}
			}
		}
		setGoingToURL(session, invocation);
		return "login";
	}

	private void setGoingToURL(Map session, ActionInvocation invocation) {
		String url = "";
		String namespace = invocation.getProxy().getNamespace();
		if (!"".equals(namespace) && !namespace.equals("/")) {
			url = url + namespace;
		}
		String actionName = invocation.getProxy().getActionName();
		if (!"".equals(actionName)) {
			url = url + "/" + actionName + ".action";
		}
		session.put(GOING_TO_URL_KEY, url);
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
